def loadDataSet():
    postingList = [['my', 'dog', 'has', 'flea', 
                    'problems', 'help', 'please'],
                  ['maybe', 'not', 'take', 'him', 
                    'to', 'dog', 'park', 'stupid'],
                  ['my', 'dalmation', 'is', 'so', 'cute', 
                    'I', 'love', 'him'],
                  ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                  ['mr', 'licks', 'ate', 'my', 'steak', 'how', 
                    'to', 'stop', 'him'],
                  ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    classVec = [0, 1, 0, 1, 0, 1]  # 1 代表侮辱性文字，0 代表正常言论
    return postingList, classVec

def createVocabList(dataSet):
    vocabSet = set([])
    for document in dataSet:
        vocabSet = vocabSet | set(document)  # 创建两个集合的并集
    return list(vocabSet)

def setOfWords2Vec(vocabList, inputSet):
    returnVec = [0] * len(vocabList)  # 创建一个其中所有元素都为 0 的向量
    for word in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)] = 1
        else:
            print("the word: %s is not in my Vocabulary!" % word)
    return returnVec

def bagOfWords2Vec(vocabList, inputSet):
    returnVec = [0] * len(vocabList)  # 创建一个其中所有元素都为 0 的向量
    for word in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)] += 1
        else:
            print("the word: %s is not in my Vocabulary!" % word)
    return returnVec

from numpy import ones,log,array
def trainNBD(trainMatrix, trainCategory,vocabList):
    numTrainDocs = len(trainMatrix)
    numWords = len(trainMatrix[0])
    pAbusive = sum(trainCategory) / float(numTrainDocs)
    p0Num = ones(numWords)  # 初始化为1，防止乘积乘上0
    p1Num = ones(numWords)  # 初始化为1，防止乘积乘上0
    p0Denom = len(vocabList)       # 原代码此处为 pOpenom (需修正为 p0Denom)
    p1Denom = len(vocabList)          # 原代码此处为 pIDenom
    
    for i in range(numTrainDocs):
        if trainCategory[i] == 1:
            p1Num += trainMatrix[i]     # 修正拼写: p1Num (原pINum)
            p1Denom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]     # 修正拼写: p0Num (原pONum)
            p0Denom += sum(trainMatrix[i])  # 修正拼写: p0Denom (原pOpenom)
    
    p1Vect = log(p1Num / p1Denom)   # 在侮辱性留言中，每个单词所占的所有单词个数的比例，这里取对数的原因是，如果乘积过小，导致最终乘积都近似等于0
    p0Vect = log(p0Num / p0Denom)   # 在非侮辱性留言中，每个单词所占的所有单词个数的比例，这里取对数的原因是，如果乘积过小，导致最终乘积都近似等于0
    return p0Vect, p1Vect, pAbusive

def classifyWB(vec2Classify, p0Vec, p1Vec, pClass1):
    pl = sum(vec2Classify * p1Vec) + log(pClass1)
    po = sum(vec2Classify * p0Vec) + log(1.0 - pClass1)
    if pl > po:
        return 1
    else:
        return 0

def testIngWB(vocabList):
    listOPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listOPosts)
    trainMat = [] 
    for postinDoc in listOPosts:
        trainMat.append(bagOfWords2Vec(myVocabList, postinDoc))
    pow, piv, pAb = trainNBD(array(trainMat), array(listClasses),vocabList)
    
    testEntry = ['love', 'my', 'daimation']
    thisDoc = array(bagOfWords2Vec(myVocabList, testEntry))
    print(testEntry, 'classified as: ', classifyWB(thisDoc, pow, piv, pAb))
    
    testEntry = ['stupid', 'garbage']
    thisDoc = array(bagOfWords2Vec(myVocabList, testEntry))
    print (testEntry, 'classified as: ', classifyWB(thisDoc, pow, piv, pAb))


listOPosts,listClasses=loadDataSet()
print(listOPosts)
print(listClasses)
myVocabList=createVocabList(listOPosts)
print(myVocabList)
#returnVec=setOfWords2Vec(myVocabList,listOPosts[0])
#print(returnVec)
#returnVec=setOfWords2Vec(myVocabList,listOPosts[3])
#print(returnVec)
trainMat=[]
for postinDoc in listOPosts:
    trainMat.append(setOfWords2Vec(myVocabList,postinDoc))
print(trainMat)
p0v,p1v,pAb=trainNBD(trainMat,listClasses,myVocabList)
print(p0v)
print(p1v)
print(pAb)
testIngWB(myVocabList)

